package me.dongcha.reggie402.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import me.dongcha.reggie402.common.R;
import me.dongcha.reggie402.entity.Employee;
import me.dongcha.reggie402.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession session) {
        String username = employee.getUsername();
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(wrapper);

        if (emp == null) {
            return R.error("用户名或密码不正确");
        }

        if (!emp.getPassword().equals(password)) {
            return R.error("用户名或密码不正确");
        }

        if (emp.getStatus() == 0) {
            return R.error("此账户已被禁用");
        }

        session.setAttribute("employee", emp.getId());

        return R.success(emp);
    }

    @PostMapping("/logout")
    public R logout(HttpSession session) {
        session.removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee, HttpSession session) {
        log.warn("新建员工基本信息{}", employee);
        employeeService.save(employee);

        return R.success("用户添加成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page<Employee> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();

        if (name != null && name.length() > 0) {
            wrapper.like(Employee::getName, name);
        }
        wrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfo, wrapper);

        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> update (@RequestBody Employee employee, HttpSession session) {
        log.warn("[要编辑的员工数据]:{}", employee);
        log.warn("[要编辑的员工id]:{}", employee.getId());

        boolean b = employeeService.updateById(employee);

        if (b) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable("id") Long id) {
        log.warn("[需要查询的id是]:" + id);
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
